home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / nqdir.c < prev    next >
C/C++ Source or Header  |  1989-10-25  |  4KB  |  169 lines

  1. /*
  2.  
  3. NQDIR.C         Aad Slingerland, oktober 1989
  4.         Arnhem, The Netherlands.
  5.  
  6. Utility to check for the existance of a specified directory
  7. on a network drive. The specified directory does not have to
  8. contain any files. This program returns an errorlevel to a
  9. batch file and in case the directory does not exist, a message
  10. to the user.
  11.  
  12. Location:       SYS:PUBLIC
  13.  
  14. Attributes:     Shareable Readonly
  15.  
  16. Invokation:     Batch file or interactive
  17.  
  18. Syntax:         NQDIR d:\path [/D]
  19.         The /D option gives debug information about
  20.         various API calls.
  21.  
  22. Return codes:   0       directory exists
  23.         1       directory not found
  24.         2       invalid command syntax
  25.  
  26. */
  27. /*
  28.     Macro definitions
  29. */
  30. #define MSC     /* For the netware PROLOG.H */
  31. /*
  32.     Header files included
  33. */
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <string.h>
  38. #include <nit.h>
  39. /*
  40.     Function prototypes for local functions
  41. */
  42. void    TellThemHow (void);
  43. /*
  44.     Main line
  45. */
  46. main    (argc, argv)
  47. int     argc;
  48. char    *argv[];
  49. {
  50.     /*
  51.         Local variables
  52.     */
  53.     int     x = 0;
  54.     int     y = 0;
  55.     int     debug = 0;
  56.     BYTE    dNum;
  57.     BYTE    dirHandle;
  58.     BYTE    sFlags;
  59.     WORD    conID;
  60.     char    oldPath[255];
  61.     char    newPath[255];
  62.     char    *z;
  63.     /*
  64.         Check for valid command line parameters
  65.     */
  66.     if (argc == 1)
  67.         {
  68.         TellThemHow ();
  69.         return (2);
  70.         }
  71.     if ( ! isalpha (*argv[1]))
  72.         {
  73.         TellThemHow ();
  74.         return (2);
  75.         }
  76.     if (( *(argv[1] + 1) != ':') || ( *(argv[1] + 2) != '\\'))
  77.         {
  78.         TellThemHow ();
  79.         return (2);
  80.         }
  81.     if (argc == 3)
  82.         if (toupper ( *(argv[2] + 1)) == 'D')
  83.         debug++;
  84.         else
  85.         {
  86.         TellThemHow ();
  87.         return (2);
  88.         }
  89.     /*
  90.         Convert the drive letter to a number, where
  91.         drive A = 0, B = 1 etc.
  92.     */
  93.     dNum = toupper (*argv[1]) - 65;
  94.     /*
  95.         Get the directory handle for the specified drive.
  96.     */
  97.     sFlags = GetDriveInformation (dNum, &conID, &dirHandle);
  98.     if (debug)
  99.         {
  100.         printf ("\n");
  101.         printf ("Shell function     : GetDriveInformation\n");
  102.         printf ("Statusflags        : %d\n", sFlags);
  103.         printf ("ConnectionID       : %d\n", conID);
  104.         printf ("DirectoryHandle    : %d\n", dirHandle);
  105.         }
  106.     /*
  107.         Get the current directory path for the specified handle.
  108.     */
  109.     x = GetDirectoryPath (dirHandle, oldPath);
  110.     if (debug)
  111.         {
  112.         printf ("\n");
  113.         printf ("Shell function     : GetDirectoryPath\n");
  114.         printf ("returncode         : %d\n", x);
  115.         printf ("Current Path       : %s\n", oldPath);
  116.         }
  117.     /*
  118.         Build a the newPath string, because it must
  119.         start with a volume name, not a drive letter.
  120.     */
  121.     z = strchr (oldPath, ':');
  122.     x = z - oldPath + 1;
  123.     strncpy (newPath, oldPath, x);
  124.     newPath[x] = '\0';
  125.     strcat (newPath, argv[1] + 3);
  126.     /*
  127.         Set a new directory (command line parm) to this
  128.         handle. If oke, the directory exists.
  129.     */
  130.     x = SetDirectoryHandle (dirHandle, newPath, dirHandle);
  131.     if (debug)
  132.         {
  133.         printf ("\n");
  134.         printf ("Shell function     : SetDirectoryHandle\n");
  135.         printf ("New specified path : %s\n", newPath);
  136.         printf ("returncode         : %d\n", x);
  137.         }
  138.     if (x == 0)
  139.         {
  140.         /*
  141.         Restore old path again
  142.         */
  143.         y = SetDirectoryHandle (dirHandle, oldPath, dirHandle);
  144.         if (debug)
  145.         {
  146.         printf ("\n");
  147.         printf ("Shell function     : SetDirectoryHandle\n");
  148.         printf ("New specified path : %s\n", oldPath);
  149.         printf ("returncode         : %d\n", x);
  150.         }
  151.         }
  152.     else
  153.         {
  154.         printf ("\nDirectory %s does not exist.\n", argv[1]);
  155.         return (1);
  156.         }
  157.     return (0);
  158. }
  159. /*
  160.     In case of an invalid command line syntax.
  161. */
  162. void    TellThemHow (void)
  163. {
  164.     printf ("\nThe correct syntax for the NQDIR command is : ");
  165.     printf ("NQDIR d:\\path [/d]\n");
  166.     printf ("The optional parameter /d gives status information\n");
  167.     printf ("about every function call to the Netware Shell.\n");
  168. }
  169.